//
//  YKMultiSelectDropdownView.m
//  YunKang
//
//  Created by leoking870 on 2017/9/29.
//  Copyright © 2017年 leoking870. All rights reserved.
//

#import "YKMultiSelectDropdownView.h"
#import "YKDropdownLabel.h"
@interface YKMultiSelectDropdownView ()
@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, assign)BOOL sameLine;
@end


@implementation YKMultiSelectDropdownView

- (instancetype)initWithTitle:(NSString *)title
                         rows:(NSArray<NSArray<NSString *> *> *)data
{
    self = [super init];
    if (self) {
        _contentTopInset = 5;
        [self addSubview:self.titleLabel];
        self.titleLabel.text = title;
        _data = data;
        NSMutableArray *views = [NSMutableArray arrayWithCapacity:data.count];
        for (NSArray<NSString *>* row in data) {
            YKDropdownLabel *dropdownView = [[YKDropdownLabel alloc]initWithText:row.firstObject row:row selectionHandler:nil];
            dropdownView.backgroundColor = [UIColor tf_colorWithHexString:@"#f2f2f2"];
            [self addSubview:dropdownView];
            [views addObject:dropdownView];
        }
        if (views.count) {
            [views mas_distributeViewsAlongAxis:MASAxisTypeHorizontal withFixedSpacing:13 leadSpacing:13 tailSpacing:13];
            [views mas_makeConstraints:^(MASConstraintMaker *make) {
                make.top.equalTo(self.titleLabel.mas_bottom).offset(5);
                make.height.mas_equalTo(30);
                make.bottom.equalTo(self.mas_bottom).offset(-5);
            }];
            [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
                make.leading.equalTo(self.mas_leading).offset(13);
                make.top.equalTo(self.mas_top).offset(5);
            }];
        }
        else {
            [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
                make.leading.equalTo(self.mas_leading).offset(13);
                make.top.equalTo(self.mas_top).offset(5);
                make.bottom.equalTo(self.mas_bottom).offset(-5);
            }];
        }
    }
    return self;
}

- (instancetype)initWithTitle:(NSString *)title
                         rows:(NSArray<NSArray<NSString *> *> *)data
                     sameLine:(BOOL)sameLine
{
    self = [super init];
    if (self) {
        _contentTopInset = 5;
        [self addSubview:self.titleLabel];
        self.titleLabel.text = title;
        self.sameLine = sameLine;
        _data = data;
        NSMutableArray *views = [NSMutableArray arrayWithCapacity:data.count];
        for (NSArray<NSString *>* row in data) {
            YKDropdownLabel *dropdownView = [[YKDropdownLabel alloc]initWithText:row.firstObject row:row selectionHandler:nil];
            dropdownView.backgroundColor = [UIColor tf_colorWithHexString:@"#f2f2f2"];
            [self addSubview:dropdownView];
            [views addObject:dropdownView];
        }
        [self layoutViews:views];
    }
    return self;
}

- (instancetype)initWithTitle:(NSString *)title
                    modelData:(NSArray<YKDropdown> *)modelData
                     sameLine:(BOOL)sameLine {
    self = [super init];
    if (self) {
        _contentTopInset = 5;
        [self addSubview:self.titleLabel];
        self.titleLabel.text = title;
        self.sameLine = sameLine;
        _modelData = modelData;
        NSMutableArray *views = [[YKDropdownLabel dropdownLabelsFromData:modelData selectionHandler:^(NSArray <YKDropdown> *selectedValues) {
            if (self.selectHandler) {
                self.selectHandler(selectedValues);
            }
        }] mutableCopy];
        for (UIView *view in views) {
            [self addSubview:view];
        }
        [self layoutViews:views];
    }
    return self;
}

- (void)setEnabled:(BOOL)enabled {
    _enabled = enabled;
    for (UIView *view in self.subviews) {
        if ([view isKindOfClass:[YKDropdownLabel class]]) {
            YKDropdownLabel *drop = (YKDropdownLabel *)view;
            drop.enabled = enabled;
            drop.backgroundColor = enabled?[UIColor whiteColor]:_disableBackgroundColor;
            drop.layer.cornerRadius = 4.f;
            drop.layer.masksToBounds = YES;
            drop.layer.borderWidth = .5f;
            drop.layer.borderColor = [UIColor tf_colorWithHexString:@"#ECECEC"].CGColor;
        }
    }
}


- (void)removeDropdownLabels {
    for (UIView *view in self.subviews) {
        if ([view isKindOfClass:[YKDropdownLabel class]]) {
            [view removeFromSuperview];
        }
    }
}

- (void)setData:(NSArray<NSArray<NSString *> *> *)data {
    _data = data;
    [self removeDropdownLabels];
    NSMutableArray *views = [NSMutableArray arrayWithCapacity:data.count];
    for (NSArray<NSString *>* row in data) {
        YKDropdownLabel *dropdownView = [[YKDropdownLabel alloc]initWithText:row.firstObject row:row selectionHandler:nil];
        dropdownView.backgroundColor = [UIColor tf_colorWithHexString:@"#f2f2f2"];
        [self addSubview:dropdownView];
        [views addObject:dropdownView];
    }

    [self layoutViews:views];
}


- (void)setModelData:(NSArray<YKDropdown> *)modelData {
    _modelData = modelData;
    [self removeDropdownLabels];

    NSMutableArray *views = [[YKDropdownLabel dropdownLabelsFromData:modelData selectionHandler:^(NSArray <YKDropdown> *selectedValues) {
        if (self.selectHandler) {
            self.selectHandler(selectedValues);
        }
    }] mutableCopy];
    for (UIView *view in views) {
        [self addSubview:view];
    }
    [self layoutViews:views];
}


- (void)setContentTopInset:(CGFloat)contentTopInset {
    _contentTopInset = contentTopInset;
    if (_sameLine) {
        [self.subviews mas_updateConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.mas_top).offset(contentTopInset);
        }];
    } else {
        [self.titleLabel mas_updateConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.mas_top).offset(contentTopInset);
        }];
        [[self.subviews tf_filteredWithPredicateBlock:^BOOL(UIView *obj, NSInteger idx) {
            return [obj isKindOfClass:[YKDropdownLabel class]];
        }] mas_updateConstraints:^(MASConstraintMaker *make) {
            make.bottom.equalTo(self.mas_bottom).offset(-contentTopInset);
        }];
    }
}

- (void)layoutViews:(NSMutableArray *)views {
    if (views.count) {
        if (self.sameLine) {
            for (UIView *view in views) {
                view.tf_weight = 3;
            }
            [views insertObject:self.titleLabel atIndex:0];
            self.titleLabel.tf_weight = 2;
            [views mas_alignWeightlyAlongAxis:MASAxisTypeHorizontal fixedSpace:@8 leadingTo:self.mas_leading leadSpace:13 trailingTo:self.mas_trailing tailSpace:13];
            [views mas_makeConstraints:^(MASConstraintMaker *make) {
                make.top.equalTo(self.mas_top).offset(5);
                make.height.mas_equalTo(30);
                make.bottom.equalTo(self.mas_bottom).offset(-5);
            }];
        }
        else {
            [views mas_distributeViewsAlongAxis:MASAxisTypeHorizontal withFixedSpacing:13 leadSpacing:13 tailSpacing:13];
            [views mas_makeConstraints:^(MASConstraintMaker *make) {
                make.top.equalTo(self.titleLabel.mas_bottom).offset(5);
                make.height.mas_equalTo(30);
                make.bottom.equalTo(self.mas_bottom).offset(-5);
            }];
            [self.titleLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
                make.leading.equalTo(self.mas_leading).offset(13);
                make.top.equalTo(self.mas_top).offset(8);
            }];
        }
    }
    else {
        [self.titleLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.leading.equalTo(self.mas_leading).offset(13);
            make.top.equalTo(self.mas_top).offset(5);
            make.bottom.equalTo(self.mas_bottom).offset(-5);
        }];
    }
}

- (void)setSelectedModels:(NSArray<YKDropdown> *)selectedModels{
    int i = 0;
    for (UIView *view in self.subviews) {
        if ([view isKindOfClass:[YKDropdownLabel class]]) {
            YKDropdownLabel *drop = (YKDropdownLabel *)view;
            if (selectedModels.count > i) {
                drop.text = selectedModels[i];
            }
            i++;
        }
    }
}

- (void)setSelectedCodes:(NSArray<NSString *> *)selectedCodes {
    
    int i = 0;
    for (UIView *view in self.subviews) {
        if ([view isKindOfClass:[YKDropdownLabel class]]) {
            YKDropdownLabel *drop = (YKDropdownLabel *)view;
            if (selectedCodes.count > i) {
                NSString *code = selectedCodes[i];
                for (id<YKDropdown> model in drop.data) {
                    if ([model.code isEqualToString:code]) {
                        drop.text = model;
                        break;
                    }
                }
            }
            i++;
        }
    }
}

- (NSArray<NSString *> *)selectedCodes {
    NSMutableArray *codes = [NSMutableArray array];
    int i = 0;
    for (UIView *view in self.subviews) {
        if ([view isKindOfClass:[YKDropdownLabel class]]) {
            YKDropdownLabel *drop = (YKDropdownLabel *)view;
            [codes addObject:drop.text.code];
            i++;
        }
    }
    return [codes copy];
}

- (NSArray<YKDropdown> *)selectedModels {
    NSMutableArray *codes = [NSMutableArray array];
    int i = 0;
    for (UIView *view in self.subviews) {
        if ([view isKindOfClass:[YKDropdownLabel class]]) {
            YKDropdownLabel *drop = (YKDropdownLabel *)view;
            [codes addObject:drop.text];
            i++;
        }
    }
    return [codes copy];
}

- (UILabel *)titleLabel {
    if (!_titleLabel) {
        _titleLabel = [[UILabel alloc] init];
        _titleLabel.textColor = [UIColor tf_colorWithHexString:@"#444444"];
        _titleLabel.font = [UIFont systemFontOfSize:14];
    }
    return _titleLabel;
}


@end
